import { withMixin } from '../../helpers/dva'

import { getTodoList } from './sever'
// import { stat } from 'fs'
// import { debug } from 'util'

export default withMixin({
  namespace: 'overview',
  state: {
    all: [],
    todoList: [],
    active: [],
    completed: []
  },
  subscriptions: {},
  effects: {
    *getTodoList({ payload }, { put, call, all, select }) {
      const { success, data } = yield call(getTodoList)
      if (!success) {
        return
      }
      yield put({
        type: 'updateState',
        payload: {
          todoList: data || [],
          all: data || []
        }
      })
    }
  },
  reducers: {
    addTodo(state, { payload }) {
      return {
        ...state,
        todoList: state.todoList.concat(payload),
        all: state.todoList
      }
    },
    delete(state, { payload }) {
      const { id } = payload
      const arr = state.todoList.filter(item => item.id !== id)
      return {
        ...state,
        todoList: arr
      }
    },
    active(state, { payload }) {
      // let all = state.todoList
      return {
        ...state,
        todoList: state.todoList.filter(item => item.completed === true)
      }
    },

    completed(state, { payload }) {
      return {
        ...state,
        todoList: state.todoList.filter(item => item.completed === false)
      }
    },

    all(state, { payload }) {
      return {
        ...state,
        todoList: state.all
      }
    }
  }
})
